# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=coin-or-coinutils
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.11.12
pkgrel=1
pkgdesc="COIN-OR collection of utility classes. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://github.com/coin-or/CoinUtils'
msys2_references=(
  'archlinux: coin-or-coinutils'
)
license=('spdx:EPL-2.0')
groups=("${MINGW_PACKAGE_PREFIX}-coin-or")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-coin-or-data-sample"
         "${MINGW_PACKAGE_PREFIX}-glpk"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-coin-or-buildtools"
             "${MINGW_PACKAGE_PREFIX}-fc")
source=("${url}/archive/releases/${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('eef1785d78639b228ae2de26b334129fe6a7d399c4ac6f8fc5bb9054ba00de64')

prepare() {
  cp -r ${MINGW_PREFIX}/share/coin-or-buildtools/BuildTools CoinUtils-releases-${pkgver}/CoinUtils

  cd CoinUtils-releases-${pkgver}/CoinUtils

  ACLOCAL="aclocal -I${srcdir}/CoinUtils-releases-${pkgver}/CoinUtils/BuildTools" \
  autoreconf -fiv
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  export lt_cv_deplibs_check_method='pass_all'

  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    export F77=${MINGW_PREFIX}/bin/gfortran.exe
  else
    export F77=${MINGW_PREFIX}/bin/flang.exe
  fi

  CXXFLAGS+=" -Wno-register" \
  ../"CoinUtils-releases-${pkgver}"/CoinUtils/configure \
    --prefix="${MINGW_PREFIX}" \
    --enable-shared \
    --disable-static \
    --with-blas-lib='-lopenblas' \
    --with-lapack-lib='' \
    --with-glpk-lib='-lglpk' \
    --enable-dependency-linking \
    ac_cv_c_compiler_gnu=yes \
    ac_cv_f77_compiler_gnu=yes \
    lt_cv_prog_gnu_ld=yes

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  make check
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  make install DESTDIR="${pkgdir}"

  install -Dm644 "${srcdir}/CoinUtils-releases-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
